package com.qingxin.park.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.service.SysUserBlockRelService;
import com.qingxin.park.service.SysUserCompRelService;
import com.qingxin.park.service.SysUserParkRelService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;

/**
 * 用户-园区（地块）关系
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/userPark")
public class UserParkRelAction extends BaseAction {
	
	@Autowired
	private SysUserCompRelService sysUserCompRelService;

	@Autowired
	private SysUserParkRelService sysUserParkRelService;
	@Autowired
	private SysUserBlockRelService sysUserBlockRelService;
	
	
	/**
	 * 为用户设置园区（地块）关联关系
	 * @param model
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/setParks.do", method = RequestMethod.GET)
	public String setParks(ModelMap model, String userid) {
		model.put("userid", userid);
		return "sys/user/setParks";
	}
	
	
	/**
	 * 获取园区-地块树数据
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSetParkData.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getSetParkData(String userid, HttpServletRequest request){
		if(StringUtils.isEmpty(userid)){
			return new ArrayList<Object>();
		}
		return sysUserBlockRelService.getSetParkData(userid, getLoginUser(request));
	}
	
	
	/**
	 * 保存用户-园区（地块）关系
	 * @param userid
	 * @param parkid
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveUserParkRels.json", method = RequestMethod.POST)
	@ResponseBody
	public Object saveUserParkRels(String userid, String[] companyid, String[] parkid, String[] blockid, HttpServletRequest request){
		if(StringUtils.isEmpty(userid)){
			return new SysError("参数错误");
		}
		
		LoginUser loginUser = getLoginUser(request);
		sysUserCompRelService.saveUserCompanyRels(userid, companyid, loginUser);
		sysUserParkRelService.saveUserParkRels(userid, parkid, loginUser);
		sysUserBlockRelService.saveUserBlockRels(userid, blockid, loginUser);
		return null;
	}
	
	
	/**
	 * 获取指定园区关联的园区用户
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value = "/getParkUserData.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getParkUserData(String parkid){
		return sysUserParkRelService.getParkUserData(parkid);
	}
	
	
	
}
